[[...path]].page.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import React from 'react';
  2. import { IUser, IUserHasId } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { useTranslation } from 'next-i18next';
  7. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  8. import dynamic from 'next/dynamic';
  9. import CountBadge from '~/components/Common/CountBadge';
  10. import PageListIcon from '~/components/Icons/PageListIcon';
  11. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  12. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  13. import { Page } from '~/components/Page';
  14. import styles from '~/components/Page/DisplaySwitcher.module.scss'; // for PageList toc style
  15. import TableOfContents from '~/components/TableOfContents';
  16. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  17. import { CrowiRequest } from '~/interfaces/crowi-request';
  18. import { RendererConfig } from '~/interfaces/services/renderer';
  19. import { IShareLinkHasId } from '~/interfaces/share-link';
  20. import {
  21. useCurrentUser, useCurrentPathname, useCurrentPageId, useRendererConfig, useIsSearchPage,
  22. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault,
  23. } from '~/stores/context';
  24. import { useDescendantsPageListModal } from '~/stores/modal';
  25. import loggerFactory from '~/utils/logger';
  26. import {
  27. CommonProps, getServerSideCommonProps, useCustomTitle, getNextI18NextConfig,
  28. } from '../utils/commons';
  29. const logger = loggerFactory('growi:next-page:share');
  30. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  31. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  32. type Props = CommonProps & {
  33. shareLink?: IShareLinkHasId,
  34. isExpired: boolean,
  35. disableLinkSharing: boolean,
  36. isSearchServiceConfigured: boolean,
  37. isSearchServiceReachable: boolean,
  38. isSearchScopeChildrenAsDefault: boolean,
  39. rendererConfig: RendererConfig,
  40. };
  41. const SharedPage: NextPage<Props> = (props: Props) => {
  42. useIsSearchPage(false);
  43. useShareLinkId(props.shareLink?._id);
  44. useCurrentPageId(props.shareLink?.relatedPage._id);
  45. useCurrentUser(props.currentUser);
  46. useCurrentPathname(props.currentPathname);
  47. useRendererConfig(props.rendererConfig);
  48. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  49. useIsSearchServiceReachable(props.isSearchServiceReachable);
  50. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  51. const { open: openDescendantPageListModal } = useDescendantsPageListModal();
  52. const { t } = useTranslation();
  53. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  54. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  55. const shareLink = props.shareLink;
  56. return (
  57. <ShareLinkLayout title={useCustomTitle(props, 'GROWI')} expandContainer={props.isContainerFluid}>
  58. <div className="h-100 d-flex flex-column justify-content-between">
  59. <header className="py-0 position-relative">
  60. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  61. </header>
  62. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  63. <div className="flex-grow-1">
  64. <div id="content-main" className="content-main">
  65. <div className="grw-container-convertible">
  66. { props.disableLinkSharing && (
  67. <div className="mt-4">
  68. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  69. </div>
  70. )}
  71. { (isNotFound && !props.disableLinkSharing) && (
  72. <div className="container-lg">
  73. <h2 className="text-muted mt-4">
  74. <i className="icon-ban" aria-hidden="true" />
  75. <span> Page is not found</span>
  76. </h2>
  77. </div>
  78. )}
  79. { (props.isExpired && !props.disableLinkSharing && shareLink != null) && (
  80. <div className="container-lg">
  81. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  82. <h2 className="text-muted mt-4">
  83. <i className="icon-ban" aria-hidden="true" />
  84. <span> Page is expired</span>
  85. </h2>
  86. </div>
  87. )}
  88. {(isShowSharedPage && shareLink != null) && (
  89. <>
  90. <ShareLinkAlert expiredAt={shareLink.expiredAt} createdAt={shareLink.createdAt} />
  91. <div className="d-flex flex-column flex-lg-row-reverse">
  92. <div className="grw-side-contents-container">
  93. <div className="grw-side-contents-sticky-container">
  94. {/* Page list */}
  95. <div className={`grw-page-accessories-control ${styles['grw-page-accessories-control']}`}>
  96. { shareLink.relatedPage.path != null && (
  97. <button
  98. type="button"
  99. className="btn btn-block btn-outline-secondary grw-btn-page-accessories
  100. rounded-pill d-flex justify-content-between align-items-center"
  101. onClick={() => openDescendantPageListModal(shareLink.relatedPage.path)}
  102. data-testid="pageListButton"
  103. >
  104. <div className="grw-page-accessories-control-icon">
  105. <PageListIcon />
  106. </div>
  107. {t('page_list')}
  108. <CountBadge count={shareLink.relatedPage.descendantCount} offset={1} />
  109. </button>
  110. ) }
  111. </div>
  112. <div className="d-none d-lg-block">
  113. <TableOfContents />
  114. </div>
  115. </div>
  116. </div>
  117. <div className="flex-grow-1 flex-basis-0 mw-0">
  118. <Page />
  119. </div>
  120. </div>
  121. </>
  122. )}
  123. </div>
  124. </div>
  125. </div>
  126. </div>
  127. </ShareLinkLayout>
  128. );
  129. };
  130. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  131. const req: CrowiRequest = context.req as CrowiRequest;
  132. const { crowi } = req;
  133. props.disableLinkSharing = crowi.configManager.getConfig('crowi', 'security:disableLinkSharing');
  134. props.isSearchServiceConfigured = crowi.searchService.isConfigured;
  135. props.isSearchServiceReachable = crowi.searchService.isReachable;
  136. props.isSearchScopeChildrenAsDefault = crowi.configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  137. props.rendererConfig = {
  138. isEnabledLinebreaks: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  139. isEnabledLinebreaksInComments: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  140. adminPreferredIndentSize: crowi.configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  141. isIndentSizeForced: crowi.configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  142. plantumlUri: process.env.PLANTUML_URI ?? null,
  143. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  144. // XSS Options
  145. isEnabledXssPrevention: crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  146. attrWhiteList: crowi.xssService.getAttrWhiteList(),
  147. tagWhiteList: crowi.xssService.getTagWhiteList(),
  148. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  149. };
  150. }
  151. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  152. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  153. props._nextI18Next = nextI18NextConfig._nextI18Next;
  154. }
  155. function getAction(props: Props): SupportedActionType {
  156. let action: SupportedActionType;
  157. if (props.isExpired) {
  158. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  159. }
  160. else if (props.shareLink == null) {
  161. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  162. }
  163. else {
  164. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  165. }
  166. return action;
  167. }
  168. async function addActivity(context: GetServerSidePropsContext, action: SupportedActionType): Promise<void> {
  169. const req: CrowiRequest = context.req as CrowiRequest;
  170. const parameters = {
  171. ip: req.ip,
  172. endpoint: req.originalUrl,
  173. action,
  174. user: req.user?._id,
  175. snapshot: {
  176. username: req.user?.username,
  177. },
  178. };
  179. await req.crowi.activityService.createActivity(parameters);
  180. }
  181. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  182. const req = context.req as CrowiRequest<IUserHasId & any>;
  183. const { crowi, params } = req;
  184. const result = await getServerSideCommonProps(context);
  185. if (!('props' in result)) {
  186. throw new Error('invalid getSSP result');
  187. }
  188. const props: Props = result.props as Props;
  189. try {
  190. const ShareLinkModel = crowi.model('ShareLink');
  191. const shareLink = await ShareLinkModel.findOne({ _id: params.linkId }).populate('relatedPage');
  192. if (shareLink != null) {
  193. props.isExpired = shareLink.isExpired();
  194. props.shareLink = shareLink.toObject();
  195. }
  196. }
  197. catch (err) {
  198. logger.error(err);
  199. }
  200. injectServerConfigurations(context, props);
  201. await injectNextI18NextConfigurations(context, props);
  202. await addActivity(context, getAction(props));
  203. return {
  204. props,
  205. };
  206. };
  207. export default SharedPage;